/**
* 
* Copyright [2007] [Sunny Liu]
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/
package ${appConfig.outputPackageName}.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import ${appConfig.outputPackageName}.controller.AbstractSpringController;
import ${appConfig.outputPackageName}.controller.ServerProcessStatus;
import ${appConfig.outputPackageName}.controller.ActionBean;
import ${appConfig.outputPackageName}.trans.TransactionProxy;

import ${appConfig.outputPackageName}.model.${entity.javaName};

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class  Spring${entity.javaName}Controller 
extends AbstractSpringController
{
	private static Logger log = LoggerFactory.getLogger(Spring${entity.javaName}Controller.class);
	private TransactionProxy transactionProxy;
	
	
	protected void findAll(ServerProcessStatus status,Object cmdObject,
			HttpServletRequest request,	HttpServletResponse response)
	{
		if(log.isDebugEnabled()) log.debug("Entering controller findAll ${entity.javaName}");
		try{
				List list = getTransactionProxy().findAll${entity.javaName}();		
				if(list==null){			
					status.setError(new Exception("Unexpected Error."));
					log.warn("Return NULL list from database");
				}else{
					status.setOutput(list);
				}	
		}catch(Exception ex){
			log.error("Failed to execute findAll()", ex);
			status.setError(ex);
		}	
		if(log.isDebugEnabled()) log.debug("Exiting controller findAll ${entity.javaName}");
	
	}
	
	protected void findByKey(ServerProcessStatus status,Object cmdObject,
	HttpServletRequest request,	HttpServletResponse response)
	{
		${entity.javaName} model = (${entity.javaName})cmdObject;		
		if(log.isDebugEnabled()) log.debug("Entering controller findByKey ${entity.javaName}=" + model);
		try{
		 	model =  getTransactionProxy().find${entity.javaName}ByKey(model);		
			if(model!=null) status.setOutput(model);
		}catch(Exception ex){
			log.error("Failed to execute findByKey()", ex);
			status.setError(ex);
		}				
		if(log.isDebugEnabled()) log.debug("Exiting controller findByKey ${entity.javaName}=" + model);
	
	}
	
	
	
	
	protected void add(ServerProcessStatus status, Object cmdObject,
			HttpServletRequest request,	HttpServletResponse response)
	{
		${entity.javaName} model = (${entity.javaName})cmdObject;
		if(!hasAllRequiredFields(request)){
			status.setOutput(model);
		}else{	
			if(log.isDebugEnabled()) log.debug("Entering controller add ${entity.javaName}=" + model);
			try{
				model =  getTransactionProxy().add${entity.javaName}(model);		
				status.setOutput(model);
			}catch(Exception ex){
				log.error("Failed to execute add()", ex);
				status.setError(ex);
			}	
		}	
		if(log.isDebugEnabled()) log.debug("Exiting controller add ${entity.javaName}=" + model);
	}
	
	
	protected void update(ServerProcessStatus status, Object cmdObject,
			HttpServletRequest request,	HttpServletResponse response)
	{
		${entity.javaName} model = (${entity.javaName})cmdObject;
		if(log.isDebugEnabled()) log.debug("Entering controller update ${entity.javaName}=" + model);
		try{
			model =  getTransactionProxy().update${entity.javaName}(model);		
			status.setOutput(model);
		}catch(Exception ex){
			log.error("Failed to execute update()", ex);
			status.setError(ex);
		}
		if(log.isDebugEnabled()) log.debug("Exiting controller update ${entity.javaName}=" + model);
	
	}
	
	
	
	protected void delete(ServerProcessStatus status, Object cmdObject,
			HttpServletRequest request,	HttpServletResponse response)
	{
		${entity.javaName} model = (${entity.javaName})cmdObject;
		if(log.isDebugEnabled()) log.debug("Entering controller delete ${entity.javaName}=" + model);
		try{
			model = getTransactionProxy().delete${entity.javaName}(model);		
			List list = getTransactionProxy().findAll${entity.javaName}();		
			status.setMessage("delete ${entity.javaName} from database successfully.");
			if(list!=null){
				status.setOutput(list);
			}			
		}catch(Exception ex){
			log.error("Failed to execute delete()", ex);
			status.setError(ex);
		}
		if(log.isDebugEnabled()) log.debug("Exiting controller delete  ${entity.javaName}=" + model);
	
	}
	
	protected boolean hasAllRequiredFields(HttpServletRequest request)
	{
		String[] paramNames = new String[]{<#list entity.primaryKeyColumns as attribute>"${attribute.javaName}"<#if attribute_has_next>, </#if></#list>};
		for(int i=0; i<paramNames.length; i++)
		{
			if(request.getParameter(paramNames[i])==null){
				return false;
			}
		}
		return true;
	}
	
	public TransactionProxy getTransactionProxy()
	{
		return this.transactionProxy;
	}
	
	public void setTransactionProxy(TransactionProxy proxy)
	{
		this.transactionProxy = proxy;
	}
	
	protected String getViewName()
	{
		return "${entity.javaName}";
	}
	protected Object getCommandObject()
	{
		return new ${entity.javaName}();
	}
}